package net.huashitong.Indicators.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Indicators.databean.CompletionClassificationDatabean;
import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.filter.CompletionClassificationFilter;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.Indicators.utils.DataUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;
//import net.huashitong.Indicators.utils.DateUtil;
import net.huashitong.Indicators.utils.IndustryNameUtils;
import net.huashitong.scheduling.entity.Resources;
import net.huashitong.scheduling.filter.ResourcesFilter;
import net.huashitong.scheduling.service.PreemptiveService;
import net.huashitong.scheduling.service.ResourcesService;
import net.huashitong.scheduling.web.PreemptiveController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/economicOperationData/")
public class DonghuaDataController extends JsonBaseController {
    @Autowired
    private ResourcesService resourcesService;
    @Autowired
    private IndicatorBreakdownService indicatorBreakdownService;
    @Autowired
    private PreemptiveService preemptiveService;
    public static final String[] years={"2018","2017","2016"};
    @Override
    protected String getView(String arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 资源清单
     * @return
     */
    @RequestMapping("/queryInformation")
    @ResponseBody
    public Message queryTypeValue(){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        ResourcesFilter filter=new ResourcesFilter();
        filter.setType("0");
        List<Resources> lists = resourcesService.queryByType(filter);
        for(Resources r:lists){
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("title",r.getTitle());
            map1.put("text",r.getText());
            list.add(map1);
        }
        map.put("name","资源清单");
        map.put("data",list);
        return coverMessage("200", "获取成功", map);
    }

    /**
     *主要经济指标
     * @return
     */
    @RequestMapping("/queryIndustryNameList")
    @ResponseBody
    public Message queryIndustryNameList(){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        map.put("title2", "重点项目情况");
        map.put("name", "项目总数：129项目");
        map.put("name2", "新开工项目：79项");
        map.put("name3", "在建项目：50项目");
        String[] industryNames = {
                "地区生产总值","固定资产投资"
        };
        DecimalFormat df=new DecimalFormat("#.##");
        for (int i = 0; i < industryNames.length; i++) {
            Map<String, Object> maps = new HashMap<String, Object>();
            IndicatorBreakdownFilter filter=new IndicatorBreakdownFilter();
            filter.setArea("永泰县");
            filter.setIndustryName(industryNames[i]);
            filter=IndustryNameUtils.changeId(filter);
            String judge = DataUtils.getValue(filter);
            String date = DataUtils.getValueMonth(judge,filter);
            List breakdown = indicatorBreakdownService.queryAll(DateForIndUtils.getMonth(date),industryNames[i],filter.getArea());
            Object[] object = (Object[]) breakdown.get(0);

            if("地区生产总值".equals(industryNames[i])){
                maps.put("targetCumulative","184.1");
                maps.put("targetRatio","11");
            }else if("固定资产投资".equals(industryNames[i])){
                maps.put("targetCumulative","134");
                maps.put("targetRatio","20");
            }else if("建筑业增加值".equals(industryNames[i])){
                maps.put("targetCumulative","504");
                maps.put("targetRatio","15");
            }else if("社会消费品零售总额".equals(industryNames[i])){
                maps.put("targetCumulative","70");
                maps.put("targetRatio","16.5");
            }else if("规模以上工业增加值".equals(industryNames[i])){
                maps.put("targetCumulative","72.7");
                maps.put("targetRatio","13");
            }else if("一般公共预算收入".equals(industryNames[i])){
                maps.put("targetCumulative","16.4");
                maps.put("targetRatio","17.7");
            }
            maps.put("industryId", object[0]);
            maps.put("cumulative",object[3]);
            maps.put("ratio", object[1]);
            //完成数
            String val =maps.get("cumulative").toString();
            Double double1 = Double.parseDouble(val);
            Double double3 = Double.parseDouble(maps.get("targetCumulative").toString());
            double1 = double1/10000;
            maps.put("cumulativeState","0");
			/*if(double1>=double3){
				 maps.put("cumulativeState","0");
			}else if(double1<double3){
				 maps.put("cumulativeState","1");
			}*/
            String st= "";
            if(double1 < 1){
                maps.put("cumulative", double1);
            }else {
                st=df.format(double1);
                maps.put("cumulative", st);
            }
            if("规模以上工业增加值".equals(industryNames[i])){
                maps.put("cumulative","-");
            }
            Double double2 = Double.parseDouble(maps.get("ratio").toString());
            Double double4 = Double.parseDouble(maps.get("targetRatio").toString());
            if(double2>=double4){
                maps.put("ratioState","0");
            }else if(double1<double3){
                maps.put("ratioState","1");
            }
            if(double2>1){
                st=df.format(double2);
                maps.put("ratio", st);
            }else {
                st=df.format(double2);
                maps.put("ratio", st);
            }

            if("社会消费品零售总额".equals(industryNames[i])){
                maps.put("industryNames","全社会消费品零售总额");
            }else{
                maps.put("industryNames",industryNames[i]);
            }
            if("一般公共预算收入".equals(industryNames[i])){
                maps.put("industryNames","一般公共预算收入");
            }
            maps.put("unit", object[6]);
            list.add(maps);
        }
        map.put("name","主要经济指标");
        map.put("lists", list);
        return coverMessage("200", "获取成功", map);
    }
    @ResponseBody
    @RequestMapping("/queryRank")
    public Message queryRank(){
        IndicatorBreakdownFilter filter=new IndicatorBreakdownFilter();
        filter.setArea("永泰县");
        String judge1 = DataUtils.getValue(filter);
        String date1 = DataUtils.getValueMonth(judge1,filter);
        filter.setDate(date1);

        filter.setDate(DateForIndUtils.getMonth(filter.getDate()));
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        String[] industryNames = {
                "地区生产总值","第一产业增加值","第二产业增加值","第三产业增加值","建筑业增加值","社会消费品零售总额","规模以上工业增加值","固定资产投资","工业固投","一般公共预算总收入","一般公共预算收入","进出口总值","实际利用外资"/*,"农林牧渔业总产值"*/,"城镇居民人均可支配收入","农村居民人均可支配收入"
        };
        for (int i = 0; i < industryNames.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            List breakdown = indicatorBreakdownService.queryAll(filter.getDate(),industryNames[i],filter.getArea());
            String mData="01";
            String siz="0";
            if(ObjectUtils.isBlank(breakdown)){
                if("1".equals(filter.getState())){
                    siz="1";
                    filter.setIndustryName(industryNames[i]);
                    filter=IndustryNameUtils.changeId(filter);
                    String  judge = DataUtils.getValue(filter);
                    String date = DataUtils.getValueMonth(judge,filter);
                    breakdown = indicatorBreakdownService.queryAll(DateForIndUtils.getMonth(date),industryNames[i],filter.getArea());
                    mData=date.substring(date.length()-2,date.length());
                }else{
                    continue;
                }
            }
            Object[] object = (Object[]) breakdown.get(0);
            Double double1 = Double.parseDouble(object[1].toString());
            Double double2 = Double.parseDouble(object[5].toString());
            BigDecimal data1 = new BigDecimal(double1);
            BigDecimal data2 = new BigDecimal(double2);
            int k = data1.compareTo(data2);
            if(k<0){
                map.put("reach", "0");
            }else {
                if("1".equals(map.get("rank"))||"2".equals(map.get("rank"))||"3".equals(map.get("rank"))){
                    map.put("reach", "2");
                }else {
                    map.put("reach", "1");
                }
            }
            map.put("industryId", object[0]);
            map.put("ratio", object[1]);
            if("1".equals(siz)&&"1".equals(filter.getState())){
                if("03".equals(mData)){
                    map.put("industryName", object[2]+"(第一季度)");
                }else if("06".equals(mData)){
                    map.put("industryName", object[2]+"(第二季度)");
                }else if("09".equals(mData)){
                    map.put("industryName", object[2]+"(第三季度)");
                }else if("12".equals(mData)){
                    map.put("industryName", object[2]+"(第四季度)");
                }

            }else{
                map.put("industryName", object[2]);
            }
            map.put("cumulative", object[3]);

            if("规模以上工业增加值".equals(industryNames[i])&&"201701-11".equals(filter.getDate())){
                map.put("rank","5");
            }else if("规模以上工业增加值".equals(industryNames[i])&&"201801-02".equals(filter.getDate())){
                map.put("rank","11");
            }

            if("社会消费品零售总额".equals(industryNames[i])&&"201701-12".equals(filter.getDate())){
                map.put("rank","5");
            }else if("社会消费品零售总额".equals(industryNames[i])&&"201801-02".equals(filter.getDate())){
                map.put("rank","6");
            }else{
                map.put("rank", object[4]==null?"-":object[4].toString());
            }

            if("第一产业增加值".equals(industryNames[i])&&"201701-12".equals(filter.getDate())){
                map.put("rank","1");
            }

            if("城镇居民人均可支配收入".equals(industryNames[i])||"农村居民人均可支配收入".equals(industryNames[i])){
                map.put("unit", "元");
            }else{
                map.put("unit", object[6]);
            }
            if("规模以上工业产值".equals(industryNames[i])){
                map.put("rank","-");
            }
			/* if("01".equals(mData)&&!"进出口总值".equals(industryNames[i])){
				 map.put("rank", "0");
			 }*/
            if("固定资产投资".equals(industryNames[i])&&"201801-02".equals(filter.getDate())){
                map.put("rank","2");
            }
            if("一般公共预算总收入".equals(industryNames[i])&&"201801-02".equals(filter.getDate())){
                map.put("rank","2");
            }
            if("一般公共预算收入".equals(industryNames[i])&&"201801-02".equals(filter.getDate())){
                map.put("rank","4");
            }
            if("规模以上工业增加值".equals(industryNames[i])&&"201801-02".equals(filter.getDate())){
                map.put("rank","11");
            }

            if("固定资产投资".equals(industryNames[i])&&"201801-03".equals(filter.getDate())){
                map.put("rank","2");
            }
            if("工业固投".equals(industryNames[i])&&"201801-03".equals(filter.getDate())){
                map.put("rank","9");
            }
            if("实际利用外资".equals(industryNames[i])&&"201801-03".equals(filter.getDate())){
                map.put("rank","-");
            }
            if("规模以上工业增加值".equals(industryNames[i])&&"201801-05".equals(filter.getDate())){
                map.put("rank","6");
            }
            list.add(map);
            }

        if(ObjectUtils.isBlank(list)){
            return coverMessage("500","暂无数据");
        }

        for (Map<String, Object> map : list) {
            map = changeName(map);
            if("亿元".equals(map.get("unit"))){
                String val =map.get("cumulative").toString();

                Double double1 = Double.parseDouble(val);
                double1 = double1/10000;
                String st= "";
                if(double1 < 1){
                    map.put("cumulative", double1);
                }else {
                    DecimalFormat df=new DecimalFormat("#.##");
                    st=df.format(double1);
                    map.put("cumulative", st);
                }
            }else{
                String val =map.get("cumulative").toString();
                Double double1 = Double.parseDouble(val);
                String st= "";
                DecimalFormat df=new DecimalFormat("#.##");
                st=df.format(double1);
                map.put("cumulative", st);
            }
            Double double2 = Double.parseDouble(map.get("ratio").toString());
            if(double2>1){
                DecimalFormat df=new DecimalFormat("#.##");
                String st=df.format(double2);
                map.put("ratio", st);
            }else {
                DecimalFormat df=new DecimalFormat("#.##");
                String st=df.format(double2);
                map.put("ratio", st);
            }

        }
        return coverMessage("200", "数据获取成功", list);
    }



    @ResponseBody
    @RequestMapping("/queryCountyPreemptive")
    public Message queryCountyPreemptive(){
        CompletionClassificationFilter filter =new  CompletionClassificationFilter();
        filter.setIndustryId("A01");
        filter.setCategory("扶贫");
        Map<String, Object> map = new HashMap<String, Object>();
        List<CompletionClassification> classifications= new ArrayList<CompletionClassification>();
        if(ObjectUtils.isBlank(filter.getYear())){
            for(int j = 0; j<PreemptiveController.years.length; j++){
                filter.setYear(years[j]);
                classifications = preemptiveService.queryCountyPreemptive(filter);
                if(ObjectUtils.isNotBlank(classifications)){
                    break;
                }
            }

        }else{
            classifications = preemptiveService.queryCountyPreemptive(filter);
        }
        List<CompletionClassificationDatabean> classificationDatabeans = new ArrayList<CompletionClassificationDatabean>();
        if(ObjectUtils.isNotBlank(classifications)){
            for (CompletionClassification completionClassification : classifications) {
                CompletionClassificationDatabean classificationDatabean = new CompletionClassificationDatabean();
                classificationDatabean.setActualCompletionValue(completionClassification.getActualCompletionValue());
                classificationDatabean.setUnit("亿元");
                classificationDatabean.setArea(completionClassification.getArea());
                classificationDatabeans.add(classificationDatabean);
            }
        }
        map.put("lists",classificationDatabeans);
        map.put("date", filter.getYear());
        return coverMessage("200", "数据获取成功", map);
    }








    public Map<String, Object> changeName(Map<String, Object> map){

        if(map.get("industryName").toString().contains("农村")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "农村人均收入"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "农村人均收入");
            }

        }
        if(map.get("industryName").toString().contains("公共预算总收入")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "公共预算总收入"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "公共预算总收入");
            }

        }
        if(map.get("industryName").toString().contains("公共预算收入")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "公共预算收入"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "公共预算收入");
            }
        }
        if(map.get("industryName").toString().contains("第一产业")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "第一产业"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "第一产业");
            }
        }
        if(map.get("industryName").toString().contains("第二产业")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "第二产业"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "第二产业");
            }
        }
        if(map.get("industryName").toString().contains("第三产业")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "第三产业"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "第三产业");
            }
        }
        if(map.get("industryName").toString().contains("农林牧渔业")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "农林牧渔业"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "农林牧渔业");
            }
        }
        if(map.get("industryName").toString().contains("城镇")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "城镇人均收入"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "城镇人均收入");
            }

        }
        if(map.get("industryName").toString().contains("规模以上工业增加值")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "规模以上工业"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "规模以上工业完成情况");
            }

        }
        if(map.get("industryName").toString().contains("建筑业")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "建筑业"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "建筑业");
            }
        }
        if(map.get("industryName").toString().contains("地区生产总值")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "GDP"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "GDP");
            }

        }
        if(map.get("industryName").toString().contains("社会消费品零售总")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "全社会消费品零售总额 "+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "全社会消费品零售总额 ");
            }
        }
        if(map.get("industryName").toString().contains("实际利用")){
            if(map.get("industryName").toString().contains("季")){
                map.put("industryName", "实际外资"+map.get("industryName").toString().substring(map.get("industryName").toString().length()-6,map.get("industryName").toString().length()));
            }else{
                map.put("industryName", "实际外资");
            }

        }

        return map;
    }
}
